<?php
class Application_Model_GioHoa extends Zend_Db_Table
{
	protected $_name='giohoa';
	
	public function getAllGioHoa(){
		try{
			$sql = 'Select * From `giohoa` where datrieng = 0';
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
			$id = $db->fetchAll ( $sql );
			return $id;
		}catch(Exception $ex){
			echo "Can not get gio hoa, Error: ".$ex->getMessage();
			exit;
		}
	}
	public function getGioHoa($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Khong tim thay gio hoa $id");
		}
		return $row->toArray();
	}//phuong thuc tra ve mot dong theo $id
	
	public function addGioHoa($loaigiohoa_id, $tengiohoa,$dongiaban,$slton,$flag,$imagename,$tiencong)
	{
		try{
			$data = array(
					'loaigiohoa_id' => $loaigiohoa_id,
					'tengiohoa' => $tengiohoa,
					'dongiaban' => (int)$dongiaban,
					'tiencongphuthu'=>$tiencong,
					'slton' => (int)$slton,
					'flag' => (int)$flag,
					'img_url' => $imagename,
					'tiencongphuthu'=>$tiencong,
			);
			$this->insert($data);
		}catch(Exception $ex){
			echo '<br/> Error when trying to add new gio hoa : '.$ex->getMessage();
			exit;
		} 
	}//insert
	public function addGioHoadatrieng($tengiohoa,$dongiaban)
	{
		
		try{
			$data = array(
					'loaigiohoa_id' => 6,
					'tengiohoa'=>$tengiohoa.'(*)',
					'dongiaban'=>$dongiaban,
					'flag' => 0,
					'datrieng'=>1,
			);
			$this->insert($data);
		}catch(Exception $ex){
			echo '<br/> Error when trying to add new gio hoa : '.$ex->getMessage();
			exit;
		}
	}
	
	public function updateGioHoa($id,$loaigiohoa_id, $tengiohoa,$dongiaban,$slton,$flag,$imagename,$tiencong)
	{
		
		$data = array(
				'loaigiohoa_id' => $loaigiohoa_id,
				'tenhoa' => $tengiohoa,
				'dongiaban' => (int)$dongiaban,
				'slton' => (int)$slton,
				'flag' => (int)$flag,
				'imagename' => $imagename,
				'tiencongphuthu'=>$tiencong,
		);
		$this->update($data, 'id = '. (int)$id);
	}//update
	
	public function updateGiaGioHoa($id,$gia){
		$data=array(
				'dongiaban'=>$gia
				);
		$this->update($data, 'id = '.$id);
	}
	
	public function updateGiohoabyarray($data)
	{
		$this->update($data, 'id ='.(int)$data['id']);
	}
	
	public function deleteGioHoa($id)
	{
		$this->delete('id =' . (int)$id);
	}//drop
	
	
	public function getGioHoaById($id){
		try{
			$sql = 'Select id From `giohoa` where id = '.(int)$id ;
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_OBJ );
			$id = $db->fetchAll ( $sql );
			return $id;
		}catch(Exception $ex){
			echo "Can not get gio hoa by id, Error: ".$ex->getMessage();
			exit;
		}
	}
	public function getGioHoaByCategory($loaigiohoa_id){
		try{
			$sql = 'Select * From `giohoa` where loaigiohoa_id = '.(int)$loaigiohoa_id ;
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
			$id = $db->fetchAll ( $sql );
			return $id;
		}catch(Exception $ex){
			echo "Can not get gio hoa by id, Error: ".$ex->getMessage();
			exit;
		}
	}
	public function getAll($loaigiohoa_id){
		return $this->fetchAll('loaigiohoa_id = ?'.(int)$loaigiohoa_id);
	}
	public function updateNumberofGioHoa($array)
	{
		foreach ($array as $key=>$value)
		{
			$giohoa=$this->getGioHoaById($key);
			$giohoa->slton=($giohoa->slton)-$value;
			$this->update($giohoa,$key);
		}
	}
	
	public function getGiaById($id_sp){
		try{
			$sql = 'Select dongiaban From `giohoa` where id = '.(int)$id_sp ;
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
			$gia = $db->fetchAll ( $sql );
			return $gia[0]['dongiaban'];
		}catch(Exception $ex){
			echo '<br/>Can not get price of gio hoa, Error: '.$ex->getMessage();
			exit;
		}
	}
	
	public function getListLoaiGioHoaIDBest_selling(){
		try{
			$sql = 'SELECT loaigiohoa_id
					FROM giohoa
					INNER JOIN ct_dondathang ON giohoa_id = id
					GROUP BY loaigiohoa_id
					ORDER BY SUM( slmua ) DESC 
					LIMIT 0 , 10';
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
			$list = $db->fetchAll( $sql );
			return $list;
		}catch(Exception $ex){
			echo '<br/>Can not get list loai gio hoa id best selling, Error: '.$ex->getMessage();
			exit;
		}
	}
	public function getLastID(){
		$qr='select id from giohoa where id>=all(select id from giohoa)';
		//echo "here";die;
		$db = $this->_db;
		$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
		$row =$db->fetchAll($qr);
		if (!$row) {
			throw new Exception("Khong tim thay gio hoa ");
		}
		return $row[0]['id'];
	}
	
	public function getresultsearch($key){
		try{
			$sql = "SELECT * 
					FROM giohoa
					WHERE datrieng = 0 and (tengiohoa LIKE  '%".$key."%'
					OR dongiaban =".(int)$key.")
					LIMIT 0 , 30";
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
			$list = $db->fetchAll( $sql );
			return $list;
		}catch(Exception $ex){
			echo '<br/>Can not get list loai gio hoa id best selling, Error: '.$ex->getMessage();
			exit;
		}
	}
}